import pandas as pd
import time
import datetime
import mysql.connector
from mysql.connector import Error




def insertdata2work_record(record):

    # 连接到MySQL数据库
    conn = mysql.connector.connect(
        host='localhost',  # 通常是 'localhost'
        user='root',  # 你的数据库用户名
        password='qy777777',  # 你的数据库密码
        database='SmartBadge'  # 你的数据库名
    )

    cursor = conn.cursor()

    # current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())

    current_time = time.strftime('%H:%M:%S', time.localtime())

    # 创建SQL插入语句
    sql = "INSERT INTO work_record (time_period, work_content) VALUES (%s, %s)"
    val = (current_time, record)

    # 执行SQL语句
    cursor.execute(sql, val)

    # 提交到数据库执行
    conn.commit()

    print(cursor.rowcount, "record inserted.")

    # 关闭Connection
    conn.close()



def query_work_records():
    try:
        mydb = mysql.connector.connect(
            host="localhost",
            user="root",
            password="qy777777",
            database="SmartBadge"
        )
        cursor = mydb.cursor(dictionary=True)

        # 查询特定字段
        sql = "SELECT time_period, work_content FROM work_record"
        cursor.execute(sql)
        results = cursor.fetchall()

        print("工作记录查询结果:")
        for record in results:
            print(f"时间段: {record['time_period']}, 工作内容: {record['work_content']}")

        return results

    except Error as e:
        print(f"查询失败: {e}")
        return []
    finally:
        if mydb.is_connected():
            cursor.close()
            mydb.close()













